/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.Material;
import org.crystalrange.enterprise.model.Pricelist;
import org.crystalrange.enterprise.model.PricelistItem;
import org.crystalrange.enterprise.model.Vendor;
import org.crystalrange.enterprise.service.PricelistService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;

public class PricelistController extends BaseController {

    protected PricelistService pricelistService;
    protected Pricelist pricelist;
    protected PricelistItem selectedPricelistItem;
    protected Listbox listboxPricelistItem;
    protected Bandbox bandboxMaterial;

    public PricelistService getPricelistService() {
        return this.pricelistService;
    }

    public void setPricelistService(PricelistService pricelistService) {
        this.pricelistService = pricelistService;
    }

    public Pricelist getPricelist() {
        return this.pricelist;
    }

    public void setPricelist(Pricelist pricelist) {
        this.pricelist = pricelist;
    }

    public PricelistItem getSelectedPricelistItem() {
        return this.selectedPricelistItem;
    }

    public void setSelectedPricelistItem(PricelistItem selectedPricelistItem) {
        this.selectedPricelistItem = selectedPricelistItem;
        
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.pricelist = this.pricelistService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Pricelist: " + this.pricelist.getName());
        } else {
            this.pricelist = this.pricelistService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Pricelist");
        }

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemPricelistItemAdd(Event event) throws Exception {
        this.pricelistItemAdd();
    }

    public void onClick$menuitemPricelistItemRemove(Event event) throws Exception {
        this.pricelistItemRemove();
    }

    public void onClick$menuitemPricelistSave(Event event) throws Exception {
        this.pricelistSave();
    }

    public void onClick$menuitemPricelistPrint(Event event) throws Exception {
        this.pricelistPrint();
    }

    public void onClick$toolbarbuttonPricelistItemAdd(Event event) throws Exception {
        this.pricelistItemAdd();
    }

    public void onClick$toolbarbuttonPricelistItemRemove(Event event) throws Exception {
        this.pricelistItemRemove();
    }

    public void onClick$toolbarbuttonPricelistSave(Event event) throws Exception {
        this.pricelistSave();
    }

    public void onClick$toolbarbuttonPricelistPrint(Event event) throws Exception {
        this.pricelistPrint();
    }

    public void onOpen$bandboxVendor(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/vendorSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Vendor selectedVendor = (Vendor) this.execution.getAttribute("selectedVendor");
            this.pricelist.setVendor(selectedVendor);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxMaterial(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/materialSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Material selectedMaterial = (Material) this.execution.getAttribute("selectedMaterial");
            this.getSelectedPricelistItem().setMaterial(selectedMaterial);
        }

        this.binder.loadAll();
    }

    private void pricelistItemAdd() {
        this.selectedPricelistItem = new PricelistItem();
        this.selectedPricelistItem.setPricelist(this.pricelist);
        this.pricelist.getItems().add(this.selectedPricelistItem);
        this.binder.loadAll();
    }

    public void pricelistItemRemove() {
        this.selectedPricelistItem.setPricelist(null);
        this.pricelist.getItems().remove(this.selectedPricelistItem);
        this.selectedPricelistItem = null;
        this.binder.loadAll();
    }

    private void pricelistSave() throws Exception {
        this.pricelistService.save(this.pricelist,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }

    private void pricelistPrint() throws Exception {
        this.pricelistService.save(this.pricelist,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.execution.sendRedirect("~./zul/html/pricelistPrintout.zul?Id=" + this.pricelist.getId().toString());
    }
}
